package com.igeek.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.entity.Assets;
import com.igeek.boot.entity.Shelves;
import com.igeek.boot.entity.Warehouses;
import com.igeek.boot.mapper.ShelvesMapper;
import com.igeek.boot.service.IAssetsService;
import com.igeek.boot.service.IShelvesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.boot.service.IWarehousesService;
import com.igeek.boot.vo.PageQueryVO;
import com.igeek.boot.vo.ShelfVO;
import com.mchange.lang.IntegerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jwj
 * @since 2023-12-26
 */
@Service
@Slf4j
public class ShelvesServiceImpl extends ServiceImpl<ShelvesMapper, Shelves> implements IShelvesService {

    @Autowired
    private IAssetsService assetsService;


    //按条件对货架表进行分页查询
    @Override
    @Transactional(readOnly = true)
    @CachePut(cacheNames = "Shelves",keyGenerator = "keyGenerator")
    public Page<Shelves> findPage(PageQueryVO pageQueryVO) {
        //获取分页条件
        Integer currentPage = pageQueryVO.getCurrentPage();
        Integer pageSize = pageQueryVO.getPageSize();
        Page<Shelves> page = new Page<>(currentPage, pageSize);

        //封装查询条件
        String queryString = pageQueryVO.getQueryString();
        LambdaQueryWrapper<Shelves> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //按照货架的名称和货架的类型进行模糊查询
        lambdaQueryWrapper.or().like(StringUtils.hasLength(queryString), Shelves::getShelfName, queryString)
                .or().like(StringUtils.hasLength(queryString), Shelves::getShelfType, queryString);

        //执行查询
        Page<Shelves> shelvesPage = baseMapper.selectPage(page, lambdaQueryWrapper);

        return shelvesPage;
    }

    //根据仓库ID查询货架表的所有信息
    @Override
    @Transactional(readOnly = true)
    public List<Shelves> findAllByWarehouseId(Integer warehouseId) {
        LambdaQueryWrapper<Shelves> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(warehouseId),Shelves::getWarehouseId, warehouseId);
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    //查询货架表的所有信息
    @Override
    @Transactional(readOnly = true)
    public List<Shelves> findAll() {
        return baseMapper.selectList(null);
    }

    //删除货架信息
    @Override
    @Transactional
    public boolean delShelves(Integer id) {
        //首先需要查询该货架是否为空  不为空不能删除货架
        List<Assets> list = assetsService.findAllByShelfId(id);
        if(CollectionUtils.isEmpty(list)){
            return baseMapper.deleteById(id) > 0;
        }
        return false;
    }

    //修改货架信息
    @Override
    @Transactional
    public boolean updateShelvesBaseInfo(Shelves shelves) {
        //判断货架是否存在
        if(baseMapper.selectCount(new LambdaQueryWrapper<Shelves>()
               .eq(Shelves::getShelfName, shelves.getShelfName())
                .eq(Shelves::getShelfType, shelves.getShelfType())
                .eq(Shelves::getStorageSpace,shelves.getStorageSpace())
                .eq(Shelves::getWarehouseId,shelves.getWarehouseId())
        )
                ==0){
            return baseMapper.updateById(shelves) > 0;
        }
        return false;
    }

    //根据id查询用户信息
    @Override
    @Transactional(readOnly = true)
    public Shelves findOneById(Integer id) {
        return baseMapper.selectById(id);
    }

    //添加货架信息
    @Override
    @Transactional(readOnly = true)
    public boolean addShelvesInfo(Shelves shelves) {
        //1.首先判断当前的货架名称、类型、所属仓库是否已经存在
        if(baseMapper.selectCount(new LambdaQueryWrapper<Shelves>()
                .eq(Shelves::getShelfName, shelves.getShelfName())
                .eq(Shelves::getShelfType, shelves.getShelfType())
                .eq(Shelves::getStorageSpace,shelves.getStorageSpace())
                .eq(Shelves::getWarehouseId,shelves.getWarehouseId())
        )==0){
            //执行添加操作
            return baseMapper.insert(shelves) > 0;
        }
        return false;
    }


    //树形选择器  -->  查询货架的所有信息及对应的物资信息
    @Override
    @Cacheable(cacheNames = "Shelves",keyGenerator = "keyGenerator")
    @Transactional(readOnly = true)
    public List<ShelfVO> findAllShelfAndAssets() {
        //1.调用查询所有货架信息
        List<Shelves> shelves = findAll();
        //2.先遍历封装shelfVO一级菜单
        List<ShelfVO> collect1 = shelves.stream().map(item -> {
            ShelfVO shelfVO = new ShelfVO();
            shelfVO.setValue(item.getShelfID());
            shelfVO.setLabel(item.getShelfName());
            return shelfVO;
        }).collect(Collectors.toList());
        //3.根据货架的id使用物资表查询对应的物资信息
        List<ShelfVO> shelfVOS = collect1.stream().map(item -> {
            List<Assets> assets = assetsService.findAllByShelfId(item.getValue());
            if (!CollectionUtils.isEmpty(assets)) {
                List<ShelfVO> collect2 = assets.stream().map(item2 -> {
                    ShelfVO shelfVO = new ShelfVO();
                    shelfVO.setValue(item2.getAssetID());
                    shelfVO.setLabel(item2.getAssetType()+"-"+item2.getAssetName());
                    return shelfVO;
                }).collect(Collectors.toList());
                item.setChildren(collect2);
            }
            //为区别以及和二级菜单的重复性
            item.setValue(item.getValue()+10000);
            return item;
        })
                .filter(item ->!CollectionUtils.isEmpty(item.getChildren()))
                .collect(Collectors.toList());
        return shelfVOS;
    }
}
